package com.share.rules.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.utils.bean.BeanUtils;
import com.share.rules.api.domain.FeeRule;
import com.share.rules.api.domain.FeeRuleRequestForm;
import com.share.rules.api.domain.FeeRuleResponseVo;
import com.share.rules.config.DroolsHelper;
import com.share.rules.domain.FeeRuleRequest;
import com.share.rules.domain.FeeRuleResponse;
import com.share.rules.mapper.FeeRuleMapper;
import com.share.rules.service.IFeeRuleService;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @return 13141
 * 2025/10/11
 * 14:34
 */
@Slf4j
@Service
public class FeeRuleServiceImpl extends ServiceImpl<FeeRuleMapper, FeeRule> implements IFeeRuleService
{
    @Autowired
    private FeeRuleMapper feeRuleMapper;

    @Autowired
    private KieContainer kieContainer;


    @Override
    public List<FeeRule> selectFeeRuleList(FeeRule feeRule)
    {
        return feeRuleMapper.selectFeeRuleLis(feeRule);
    }

    @Override
    public List<FeeRule> getALLFeeRuleList() {
        return feeRuleMapper.selectList(new LambdaQueryWrapper<FeeRule>().eq(FeeRule::getStatus, "1"));
    }

    // @Override
    // public FeeRuleResponseVo calculateOrderFee(FeeRuleRequestForm feeRuleRequestForm) {
    //     //封装传入对象
    //     FeeRuleRequest feeRuleRequest = new FeeRuleRequest();
    //     feeRuleRequest.setDurations(feeRuleRequestForm.getDuration());
    //     log.info("传入参数：{}", JSON.toJSONString(feeRuleRequest));
    //
    //     //获取最新订单费用规则
    //     FeeRule feeRule = feeRuleMapper.selectById(feeRuleRequestForm.getFeeRuleId());
    //     KieSession kieSession = DroolsHelper.loadForRule(feeRule.getRule());
    //
    //     //封装返回对象
    //     FeeRuleResponse feeRuleResponse = new FeeRuleResponse();
    //     kieSession.setGlobal("feeRuleResponse", feeRuleResponse);
    //     // 设置订单对象
    //     kieSession.insert(feeRuleRequest);
    //     // 触发规则
    //     kieSession.fireAllRules();
    //     // 中止会话
    //     kieSession.dispose();
    //     log.info("计算结果：{}", JSON.toJSONString(feeRuleResponse));
    //
    //     //封装返回对象
    //     FeeRuleResponseVo feeRuleResponseVo = new FeeRuleResponseVo();
    //     feeRuleResponseVo.setTotalAmount((feeRuleResponse.getTotalAmount()));
    //     feeRuleResponseVo.setFreePrice((feeRuleResponse.getFreePrice()));
    //     feeRuleResponseVo.setExceedPrice((feeRuleResponse.getExceedPrice()));
    //     feeRuleResponseVo.setFreeDescription(feeRuleResponse.getFreeDescription());
    //     feeRuleResponseVo.setExceedDescription(feeRuleResponse.getExceedDescription());
    //     return feeRuleResponseVo;
    // }

    // 计算订单费用
    @Override
    public FeeRuleResponseVo calculateOrderFee(FeeRuleRequestForm feeRuleRequestForm) {
        //封装传入对象
        FeeRuleRequest feeRuleRequest = new FeeRuleRequest();
        feeRuleRequest.setDurations(feeRuleRequestForm.getDuration());
        log.info("传入参数：{}", JSON.toJSONString(feeRuleRequest));

        // 开启会话
        KieSession kieSession = kieContainer.newKieSession();

        //封装返回对象
        FeeRuleResponse feeRuleResponse = new FeeRuleResponse();
        kieSession.setGlobal("feeRuleResponse", feeRuleResponse);
        // 设置订单对象
        kieSession.insert(feeRuleRequest);
        // 触发规则
        kieSession.fireAllRules();
        // 中止会话
        kieSession.dispose();
        log.info("计算结果：{}", JSON.toJSONString(feeRuleResponse));

        //封装返回对象
        FeeRuleResponseVo feeRuleResponseVo = new FeeRuleResponseVo();
        BeanUtils.copyProperties(feeRuleResponse, feeRuleResponseVo);
        return feeRuleResponseVo;
    }

}
